<?php
/**
 * Created by PhpStorm.
 * User: yxb
 * Date: 2016/12/21
 * Time: 10:07
 */
 
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Url;
header("Content-Type:text/html;charset=utf-8");
class Area extends Controller{
    /**
    天隽海专区
     */
    public function tjh(){
       $where = [
           'id' => 3,
           'is_delete' => 0
       ];
        $field = [
            'name',
            'id',
            'img'
        ];
        $data = Db::name("mall")->where($where)->field($field)->find();
        if(!empty($data)){
            return format_return(1,$data,'数据获取成功');
        }else{
            return format_return(0,'','数据获取失败');
        }
    }
    /**
    获取固定分区
     * @param m_id 商城id
     */
    public function fix_area($m_id = 0){
        empty($m_id) && $this->error("获取失败");
        $where_area = [
            'parent_id' => $m_id,
            'is_fixed' => 1
        ];
        $field = [
            'name',
            'id',
            'img'
        ];
        $data = Db::name("mall")->where($where_area)->field($field)->select();
        if(!empty($data)){
            return format_return(1,$data,'数据获取成功');
        }else{
            return format_return(0,'','数据获取失败');
        }
    }
    /**
    类似美容分区的分区
     * @param m_id 商城id
     * @param c_id 分区id
     * @param condition 0 默认 1 销量 2 价格 3 时间
     * @param sort 0 升序 1 降序
     */
     public function area($m_id = 2,$c_id = 11,$condition = 1 , $sort = 1){
        
         empty($m_id) && $this->error("获取失败");
         empty($c_id) && $this->error("获取失败");
         $where_area = [
            'id' => $c_id,
            'is_delete' => 0
        ];
        $area = Db::name("mall")->where($where_area)->find();
         $where_classification = [
             'parent_id' => $c_id,
             'is_delete' => 0
         ];
         $classification = Db::name("category")->where($where_classification)->order('sort desc')->select();
         $ids = i_array_column($classification,'id');
         if(!empty($ids)){
             $where_goods = [
                 'm_id' => $m_id,
                 'mc_id' => $c_id,
                 'is_delete' => 0,
                 'c_id' => array('in',$ids),
                 'status' => 1
             ];
             $order = '';
             switch($condition){
                 case 0:
                     $order = 'sell_num';
                     break;
                 case 1:
                     $order = 'sell_num';
                     break;
                 case 2:
                     $order = 'price';
                     break;
                 case 3:
                     $order = 'shelves_timestamp';
                     break;
                 default :
                     break;
             }
             switch($sort){
                 case 0:
                     $order .= ' asc';
                     break;
                 case 1 :
                     $order .= ' desc';
                     break;
                 default :
                     break;
             }
             $goods = Db::name("goods")->where($where_goods)->order($order)->select();
             for($j = 0; $j < sizeof($classification); $j ++){
                 for($i = 0; $i < sizeof($goods); $i ++){
                     if($classification[$j]['id'] == $goods[$i]['c_id']){
                         $data['goods'][$j][] = $goods[$i];
                         if(sizeof($data['goods'][$j]) >= 10){
                             break;
                         }
                     }
                 }
             }
         }
         var_dump($data['goods']);exit;
         $data['area'] = $area;
         $data['classification'] = $classification;
         var_dump($data);exit;
         return format_return(1,$data,"数据获取成功");
     }
    /**
    类似美容专区商品获取
     * @param m_id 商城id
     * @param c_id 分区id
     * @param page 分页
     * @param class 分类id
     * @param condition 0 默认 1 销量 2 价格 3 时间
     * @param sort 0 升序 1 降序
     */
    public function get_areagoods($m_id = 0,$c_id = 0,$page = 1,$class_id = 0,$condition = 0,$sort = 0){
        $start = 10*$page;
        $where_goods = [
            'm_id' => $m_id,
            'mc_id' => $c_id,
            'c_id' => $class_id,
            'is_delete' => 0,
            'status' => 1
        ];
        $order = '';
        switch($condition){
            case 0:
                $order = 'sell_num';
                break;
            case 1:
                $order = 'sell_num';
                break;
            case 2:
                $order = 'price';
                break;
            case 3:
                $order = 'shelves_timestamp';
                break;
            default :
                break;
        }
        switch($sort){
            case 0:
                $order .= ' asc';
                break;
            case 1:
                $order .= ' desc';
                break;
            default:
                break;
        }
        $goods = Db::name("goods")->where($where_goods)->order($order)->limit($start,10)->select();
        if(!empty($goods)){
            return format_return(1,$goods,"数据获取成功");
        }else{
            return format_return(0,'',"数据获取失败");
        }
    }
    /**
    医疗专区
     * @param m_id 商城id
     * @param c_id 分区id
     */
    public function medical_area($m_id = 0,$c_id = 0){
        empty($m_id) && $this->error("获取失败");
        empty($c_id) && $this->error("获取失败");
        $where_area = [
            'id' => $c_id,
            'is_delete' => 0
        ];
        $where_goods = [
            'm_id' => $m_id,
            'mc_id' => $c_id,
            'is_delete' => 0,
            'status' => 1
        ];
        $goods = Db::name("goods")->where($where_goods)->order('sell_num desc')->limit(16)->select();
        $where_img = [
            'm_id' => $c_id,
            'is_delete' => 0
        ];
        $img = Db::name("medical_area")->where($where_img)->find();
        $data['goods'] = $goods;
        $data['img'] = $img;
        return format_return(1,$data,"数据获取成功");
    }
    /**
    医疗专区分页
     * @param m_id 商城id
     * @param c_id 分区id
     * @param page 分页
     */
    public function medical_goods($m_id = 0,$c_id = 0,$page = 1){
        $start = 16*$page;
        $where_goods = [
            'm_id' => $m_id,
            'mc_id' => $c_id,
            'is_delete' => 0,
            'status' => 1
        ];
        $goods = Db::name("goods")->where($where_goods)->order('sell_num desc')->limit($start,16)->select();
        if(!empty($goods)){
            return format_return(1,$goods,"数据获取成功");
        }else{
            return format_return(0,'',"数据获取失败");
        }
    }

    /**
    活动专区轮播图
     * @param m_id 商城id
     * @param c_id 活动分区id
     */
    public function activity_first($m_id = 0,$c_id = 0){
        empty($m_id) && $this->error("获取失败");
        empty($c_id) && $this->error("获取失败");
        $where = [
            'a.m_id' => $m_id,
            'a.mc_id' => $c_id,
            'a.status' => 1,
            'a.type' => 0,
            'a.is_delete' => 0
        ];
        $join = [
            ['goods g','g.id=a.g_id','left']
        ];
        $field = [
            'a.m_id',
            'a.mc_id',
            'a.status',
            'a.type',
            'a.img',
            'a.g_id',
            'a.end_time',
            'a.start_time',
            'a.name',
            'g.subject'
        ];
        $img = Db::name("activity")->alias('a')->join($join)->field($field)->limit(4)->where($where)->select();
        //print_r($img);die;
        if(!empty($img)){
            foreach($img as $key => $value){
                if($value['end_time'] < time()){
                    unset($img[$key]);
                }else{
                    $img[$key]['start_time'] = date('Y-m-d H:i:s',$value['start_time']);
                    $img[$key]['end_time'] = date('Y-m-d H:i:s',$value['end_time']);

                }
            }
            return format_return(1,$img,"获取数据成功");
        }else{
            return format_return(0,'','获取数据失败');
        }
    }

    /**
    活动专区四拼图
     * @param m_id 商城id
     * @param c_id 分区id
     */
    public function activity_second($m_id = 0,$c_id = 0){
        empty($m_id) && $this->error("获取失败");
        empty($c_id) && $this->error("获取失败");
        $where = [
            'm_id' => $m_id,
            'mc_id' => $c_id,
            'status' => 1,
            'type' => 1,
            'is_delete' => 0
        ];
        $img = Db::name("activity")->where($where)->order('sort desc')->limit(4)->select();
        if(!empty($img)){
            foreach($img as $key => $value){
                if($value['end_time'] < time()){
                    unset($img[$key]);
                }else{
                    $img[$key]['start_time'] = date('Y-m-d H:i:s',$value['start_time']);
                    $img[$key]['end_time'] = date('Y-m-d H:i:s',$value['end_time']);
                }
            }
            return format_return(1,$img,"数据获取成功");
        }
    }
    /**
    活动专区条状图
     * @param m_id 商城id
     * @param c_id 分区id
     * @param page 分页
     */
    public function activity_third($m_id = 0,$c_id = 0,$page = 0){
        empty($m_id) && $this->error("获取失败");
        empty($c_id) && $this->error("获取失败");
        $start = $page*10;
        $where = [
            'm_id' => $m_id,
            'mc_id' => $c_id,
            'status' => 1,
            'type' => 2,
            'is_delete' => 0
        ];
        $img = Db::name("activity")->where($where)->order('sort desc')->limit($start,10)->select();
        if(!empty($img)){
            foreach($img as $key => $value){
                if($value['end_time'] < time()){
                    unset($img[$key]);
                }else{
                    $img[$key]['start_time'] = date('Y-m-d H:i:s',$value['start_time']);
                    $img[$key]['end_time'] = date('Y-m-d H:i:s',$value['end_time']);
                }
            }
            return format_return(1,$img,"数据获取成功");
        }else{
            return format_return(0,$img,"数据获取失败");
        }
    }
    /**
    天隽海专区轮播
     * @param c_id 分区id
     */
    public function sky_shuffling($c_id =0){
        empty($c_id) && $this->error("获取失败");
        $where = [
            'm_id' => $c_id,
            'is_delete' => 0
        ];
        $shuffling = Db::name("shuffling")->where($where)->select();
        if(!empty($shuffling)){
            return format_return(1,$shuffling,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }

    /**
    天隽海专区分类
     * @param c_id 分区id
     */
    public function sky_classification($c_id = 0){
        empty($c_id) && $this->error("获取失败");
        $where = [
            'parent_id' => $c_id,
            'is_delete' => 0
        ];
        $classification = Db::name("mall")->where($where)->order('sort desc')->select();
        if(!empty($classification)){
            return format_return(1,$classification,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    天隽海美丽咨询
     * @param c_id 分区id
     */
    public function sky_nice($c_id = 0){
        empty($c_id) && $this->error("获取失败");
        $where = [
            'is_delete' => 0,
            'type' => 4,
            'm_id' => $c_id
        ];
        $img = Db::name("mall_index")->where($where)->find();
        if(!empty($img)){
            return format_return(1,$img,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }

    /**
    天隽海拼四张
     * @param c_id 分区id
     */
    public function sky_four($c_id = 0){
        empty($c_id) && $this->error("获取失败");
        $where = [
            'is_delete' => 0,
            'type' => 1,
            'm_id' => $c_id
        ];
        $img = Db::name("mall_index")->where($where)->select();
        if(!empty($img)){
            return format_return(1,$img,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    天隽海条状图
     * @param c_id 分区id
     */
    public function sky_line($c_id = 0,$page = 0){
        empty($c_id) && $this->error("获取失败");
        $start = 8*$page;
        $where = [
            'is_delete' => 0,
            'type' => 5,
            'm_id' => $c_id
        ];
        $img = Db::name("mall_index")->where($where)->limit($start,8)->select();
        if(!empty($img)){
            return format_return(1,$img,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    商品属性
     * @param g_id 分区id
     */
    public function goods_classify($g_id = 0){
        
        $goods_color = Db::name('properties_color')->where(array('goods_id' => $g_id))->select();
        if(!empty($goods_color)){
            return format_return(1,$goods_color,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }
    /**
    商品非颜色属性
     * @param g_id 分区id
     */
    public function goods_noclassify($g_id = 0){
        $goods_nocolor = Db::name('propertise')->where(array('goods_id' => $g_id,'is_delete'=>0))->group('classify_name')->select();
        for($i=0;$i<count($goods_nocolor);$i++){
            $data[] = $goods_nocolor[$i]['classify_name'];
        }
        for($i=0;$i<count($data);$i++){
            $data[$i] = Db::name('propertise')->where(array('classify_name' =>$data[$i],'is_delete'=>0,'goods_id'=>$g_id))->select();
        }
        if(!empty($data)){
            return format_return(1,$data,"数据获取成功");
        }else{
            return format_return(0,'','数据为空');
        }
    }
}